<template>
  <a-row :gutter="16">
    <a-col :span="6" v-for="course in courses" :key="course.id">
      <a-card hoverable @click="goToCourse(course.id)">
        <template #cover>
          <img :src="course.cover" :alt="course.title" />
        </template>
        <a-card-meta :title="course.title" :description="course.description">
          <template #avatar>
            <a-avatar :src="course.teacher.avatar" />
          </template>
        </a-card-meta>
        <div class="course-info">
          <div class="price">¥{{ course.price }}</div>
          <div class="stats">
            <span>{{ course.students }}人学习</span>
            <a-tag :color="getDifficultyColor(course.difficulty)">{{ course.difficulty }}</a-tag>
          </div>
        </div>
      </a-card>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'

interface Course {
  id: number
  title: string
  description: string
  cover: string
  price: number
  students: number
  difficulty: string
  teacher: {
    name: string
    avatar: string
  }
}

interface Props {
  courses: Course[]
}

const props = defineProps<Props>()
const router = useRouter()

const goToCourse = (id: number) => {
  router.push(`/course/${id}`)
}

const getDifficultyColor = (difficulty: string) => {
  const colors = {
    '初级': 'green',
    '中级': 'orange',
    '高级': 'red'
  }
  return colors[difficulty as keyof typeof colors] || 'blue'
}
</script>

<style scoped>
.course-info {
  margin-top: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  font-size: 18px;
  font-weight: bold;
  color: #f5222d;
}

.stats {
  display: flex;
  gap: 8px;
  align-items: center;
  color: #666;
  font-size: 12px;
}
</style> 